package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.admin.service.ISystemConfigService;
import com.shop.admin.validate.SystemConfigCreateValidate;
import com.shop.admin.validate.SystemConfigSearchValidate;
import com.shop.admin.validate.SystemConfigUpdateValidate;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.vo.SystemConfigDetailVo;
import com.shop.admin.vo.SystemConfigListedVo;
import com.shop.common.core.PageResult;
import com.shop.common.entity.system.SystemConfig;
import com.shop.common.mapper.system.SystemConfigMapper;
import com.shop.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 系统全局配置实现类
 * @author DragonDean
 */
@Service
public class SystemConfigServiceImpl implements ISystemConfigService {
        
    @Resource
    SystemConfigMapper systemConfigMapper;

    /**
     * 系统全局配置列表
     *
     * @author DragonDean
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<SystemConfigListedVo>
     */
    @Override
    public PageResult<SystemConfigListedVo> list(PageValidate pageValidate, SystemConfigSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<SystemConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        systemConfigMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:type:str",
            "like:name:str",
            "=:value:str",
        });

        IPage<SystemConfig> iPage = systemConfigMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<SystemConfigListedVo> list = new LinkedList<>();
        for(SystemConfig item : iPage.getRecords()) {
            SystemConfigListedVo vo = new SystemConfigListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 系统全局配置详情
     *
     * @author DragonDean
     * @param id 主键参数
     * @return SystemConfig
     */
    @Override
    public SystemConfigDetailVo detail(String type, String name) {
        SystemConfig model = systemConfigMapper.selectOne(
                new QueryWrapper<SystemConfig>()
                    .eq("type", type)
                    .eq("name",  name)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        SystemConfigDetailVo vo = new SystemConfigDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 系统全局配置新增
     *
     * @author DragonDean
     * @param createValidate 参数
     */
    @Override
    public void add(SystemConfigCreateValidate createValidate) {
        SystemConfig model = new SystemConfig();
        model.setType(createValidate.getType());
        model.setName(createValidate.getName());
        model.setValue(createValidate.getValue());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        systemConfigMapper.insert(model);
    }

    /**
     * 系统全局配置编辑
     *
     * @author DragonDean
     * @param updateValidate 参数
     */
    @Override
    public void edit(SystemConfigUpdateValidate updateValidate) {
        SystemConfig model = systemConfigMapper.selectOne(
                new QueryWrapper<SystemConfig>()
                    .eq("type",  updateValidate.getType())
                    .eq("name",  updateValidate.getName())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(model.getId());
        model.setValue(updateValidate.getValue());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        systemConfigMapper.updateById(model);
    }

    /**
     * 系统全局配置删除
     *
     * @author DragonDean
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        SystemConfig model = systemConfigMapper.selectOne(
                new QueryWrapper<SystemConfig>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        systemConfigMapper.delete(new QueryWrapper<SystemConfig>().eq("id", id));
    }

}
